package com.mdlib.helper.module.send.profile.fragments;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.LinearLayout;

import com.ljlib.core.base.BaseFragment;
import com.ljlib.help.R;
import com.mdlib.helper.api.DataManager;
import com.mdlib.helper.api.network.RxHelper;
import com.mdlib.helper.api.network.RxSubscribe;
import com.mdlib.helper.model.entity.VoucherEntity;
import com.mdlib.helper.ui.adapter.VoucherAdapter;
import com.mdlib.helper.ui.widget.springview.DefaultFooter;
import com.mdlib.helper.ui.widget.springview.HelperHeads;
import com.mdlib.helper.ui.widget.springview.SpringView;
import com.mdlib.helper.utils.ToastUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;

/**
 * 作者：HQY on 16/11/17 11:44
 * 邮箱：hqy_xz@126.com
 * 已使用
 */

public class HasBeenUsedVoucherFragment extends BaseFragment {


    @Bind(R.id.rv_voucher_used)
    RecyclerView mRvVoucherUsed;
    @Bind(R.id.ll_voucher_null)
    LinearLayout mLlVoucherNull;
    @Bind(R.id.sv_voucher_used)
    SpringView mSvVoucherUsed;
    private List<VoucherEntity> voucherList = new ArrayList<>();

    private final String USED = "2";

    private VoucherAdapter mVoucherAdapter;

    private int pageNumber = 1;

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_has_been_used_voucher;
    }

    @Override
    protected void initView(View rootView) {
        super.initView(rootView);
        init();

    }

    public void init() {

        mVoucherAdapter = new VoucherAdapter(voucherList);
        mRvVoucherUsed.setLayoutManager(new LinearLayoutManager(getActivity()));
        mRvVoucherUsed.setAdapter(mVoucherAdapter);
        mSvVoucherUsed.setType(SpringView.Type.FOLLOW);
        mSvVoucherUsed.setListener(new SpringView.OnFreshListener() {
            @Override
            public void onRefresh() {
                pageNumber = 1;
                getUsedVoucher();
            }

            @Override
            public void onLoadmore() {
                getUsedVoucher();
            }
        });
        mSvVoucherUsed.setHeader(new HelperHeads(getActivity()));
        mSvVoucherUsed.setFooter(new DefaultFooter(getActivity()));
        getUsedVoucher();

    }

    private void getUsedVoucher() {
        startLoadingDialog(true);
        addSubscribe(DataManager.getInstance().getUserApi()
                .getDisabledVoucher(pageNumber + "", USED)
                .compose(RxHelper.<ArrayList<VoucherEntity>>defaultHandleResult())
                .subscribe(new RxSubscribe<ArrayList<VoucherEntity>>() {
                    @Override
                    protected void onSucc(ArrayList<VoucherEntity> voucherEntities) {
                        stopLoadingDialog();
                        pageNumber++;
                        voucherList = voucherEntities;
                        updateList(voucherList);
                        mSvVoucherUsed.onFinishFreshAndLoad();

                    }

                    @Override
                    protected void onFail(String msg) {
                        stopLoadingDialog();
                        ToastUtil.showToast(msg);
                        mSvVoucherUsed.onFinishFreshAndLoad();
                    }
                }));

    }

    public void updateList(List<VoucherEntity> voucher) {
        if (voucher.size() > 0 && pageNumber == 2) {
            mVoucherAdapter.setNewData(voucher);
            mVoucherAdapter.notifyDataSetChanged();
        } else if (voucher.size() > 0 && pageNumber > 2) {
            mVoucherAdapter.addData(voucher);
            mVoucherAdapter.notifyDataSetChanged();
        } else if (voucher.size() <= 0 && pageNumber > 2) {
            pageNumber--;
            ToastUtil.showToast("没有更多数据了!");
        } else {
            mLlVoucherNull.setVisibility(View.VISIBLE);
            mRvVoucherUsed.setVisibility(View.GONE);
        }
    }
}
